package com.ebei.admin.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ebei.admin.common.util.CommonUtil;
import com.ebei.admin.model.dto.OrgDTO;
import com.ebei.admin.model.dto.OrgTree;
import com.ebei.admin.model.entity.*;
import com.ebei.admin.service.SysCompanyService;
import com.ebei.admin.service.SysOrgService;
import com.ebei.admin.service.SysParkService;
import com.ebei.admin.service.SysProjectService;
import com.ebei.common.constant.CommonConstant;
import com.ebei.common.util.Result;
import com.ebei.common.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/org")
@Api(value = "组织", description = "组织架构接口")
public class OrgController extends BaseController {

    @Autowired
    private SysOrgService sysOrgService;
    @Autowired
    private SysCompanyService sysCompanyService;
    @Autowired
    private SysParkService sysParkService;
    @Autowired
    private SysProjectService sysProjectService;

    @ApiOperation(value = "根据参数加载List", notes = "根据参数加载List")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parent_id", value = "园区id", required = false),
            @ApiImplicitParam(name = "type", value = "组织类型", required = false),
    })
    @GetMapping
    public List<SysOrg> findByParentIdAndType(@RequestParam Map<String, Object> params) {
        CommonUtil.removeT(params);
        return sysOrgService.findByParentIdAndType(params);
    }

    @ApiOperation(value = "获取组织架构子节点", notes = "获取当前组织架构子节点")
    @ApiImplicitParam(name = "orgId", value = "组织架构id", required = true)
    @GetMapping("/getOrgChildren")
    public Result<List<OrgTree>> getOrgChildren(@RequestParam Integer orgId) {
        Result<List<OrgTree>> result = new Result<>();
        List<SysOrg> sysOrgs = sysOrgService.getChildren(orgId);
        result.setData(sysOrgService.getDeptTree(sysOrgs, orgId));
        result.setErrorMessage("获取成功");
        result.setErrorCode(Result.SUCCESS);
        return result;
    }

    @ApiOperation(value = "获取组织类型", notes = "获取组织类型")
    @ApiImplicitParam(name = "userId", value = "用户id", required = true)
    @GetMapping("/getOrgType")
    public Result<List<SysDictDetail>> getOrgType(@RequestParam Integer userId) {
        return sysOrgService.getOrgType(userId);
    }

    @ApiOperation(value = "新增组织架构", notes = "新增组织架构")
    @ApiImplicitParam(name = "sysOrg", value = "组织架构实体", required = true)
    @PostMapping("/addDept")
    public Result<Boolean> addDept(@RequestBody OrgDTO orgDTO) {
        return sysOrgService.cascadeInsert(orgDTO);
    }

    @ApiOperation(value = "删除组织架构", notes = "根据ID删除组织架构")
    @ApiImplicitParam(name = "id", value = "组织架构ID", required = true)
    @PostMapping("/delOrg")
    public Result<Boolean> delete(@RequestBody Map<String, Object> params) {
        return sysOrgService.delOrg(params);
    }

    @ApiOperation(value = "修改组织架构", notes = "修改组织架构相关字段")
    @ApiImplicitParam(name = "sysOrg", value = "组织实体", required = true)
    @PostMapping("/edit")
    public Result<Boolean> edit(@RequestBody OrgDTO orgDTO) throws Exception {
        Result<Boolean> result = new Result<>();
        SysOrg sysOrg = new SysOrg();
        BeanUtils.copyProperties(orgDTO, sysOrg);
        Integer type = sysOrg.getType();
        if (sysOrgService.updateById(sysOrg)) {
            if (CommonConstant.ORGTYPE_COMP.equals(type)) {
                SysCompany sysCompany = new SysCompany();
                BeanUtils.copyProperties(orgDTO, sysCompany);
                sysCompanyService.updateById(sysCompany);
            } else if (CommonConstant.ORGTYPE_SPARK.equals(type)) {
                SysPark sysPark = new SysPark();
                BeanUtils.copyProperties(orgDTO, sysPark);
                sysParkService.updateById(sysPark);
            } else if (CommonConstant.ORGTYPE_PROJECT.equals(type)) {
                SysProject sysProject = new SysProject();
                BeanUtils.copyProperties(orgDTO, sysProject);
                sysProjectService.updateById(sysProject);
            }
            result.setErrorMessage("修改组织架构成功");
            result.setErrorCode(Result.SUCCESS);
        } else {
            result.setErrorMessage("修改组织架构失败");
            result.setErrorCode(Result.FAIL);
        }
        return result;
    }

    @ApiOperation(value = "获取组织架构树(组织架构页面专用)", notes = "获取组织架构树(组织架构页面专用)")
    @ApiImplicitParam(name = "userId", value = "用户id", required = true)
    @GetMapping("/getDeptTree")
    public Result<List<OrgTree>> getDeptTree(@RequestParam Integer userId) {
        return sysOrgService.getDeptTree(userId);
    }

    /**
     * 根据组织id获取所有岗位
     *
     * @param map
     * @return Result
     */
    @ApiOperation(value = "根据组织Id获取所有岗位")
    @ApiImplicitParam(name = "orgId", value = "组织id", required = true)
    @GetMapping("/getPosition")
    public Result<Page> findPositionsByOrgId(@RequestParam Map<String, Object> map) {
        return sysOrgService.findPositionsByOrgId(map);
    }

    /**
     * 根据用户id获得所属的企业
     *
     * @param id
     * @return Result
     */
    @ApiOperation(value = "根据用户id获得所属的企业")
    @ApiImplicitParam(name = "userId", value = "用户id", required = true)
    @GetMapping("/getCompanyByUserId/{id}")
    public Result<Page> getCompanyByUserId(@PathVariable Integer id) {
        return sysOrgService.getOrgByUserId(id);
    }

    /**
     * 根据组织的orgId获得组织详情
     *
     * @param orgId
     * @return
     */
    @ApiOperation(value = "根据组织id获得组织详情")
    @ApiImplicitParam(name = "orgId", value = "组织id", required = true)
    @GetMapping("/getInfoById")
    public Result findOrgById(@RequestParam Integer orgId) {
        return sysOrgService.findOrgById(orgId);
    }


    @ApiOperation(value = "根据组织id和用户id获取组织")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgId", value = "当前组织id", required = false),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true),
            @ApiImplicitParam(name = "expectType", value = "期望类型", required = false)
    })
    @GetMapping("/getOrgByUserId")
    public Result getOrgByUserId(@RequestParam Map<String, Object> map) {
        return sysOrgService.getByUserIdAndOrgId(map);
    }

    @ApiOperation(value = "根据组织id获取子节点树")
    @ApiImplicitParam(name = "orgId", value = "当前组织id", required = true)
    @GetMapping("/getOrgTreeByOrgId")
    public Result getOrgTreeByOrgId(@RequestParam Integer orgId) {
        return sysOrgService.getOrgTreeByOrgId(orgId);
    }

    @ApiOperation(value = "获取用户能看到的园区、企业列表")
    @ApiImplicitParam(name = "userId", value = "用户id", required = true)
    @GetMapping("/getPermissionByUserId")
    public Result getPermissionByUserId(Integer userId){
        return sysOrgService.getPermissionByUserId(userId);
    }

}
